package day_2022_9_to_12.Day;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author haomin
 * @date 2022/10/05 02:24
 **/
public class Day34_1_Num811_SubdomainVisits {
    class Solution {
        public List<String> subdomainVisits(String[] cpdomains) {
            Map<String, Integer> map = new HashMap<>();
            for(int i = 0; i < cpdomains.length; ++i){
                String[] s = cpdomains[i].split(" ");
                int a = Integer.parseInt(s[0]);
                failMap(map, s[1], a);
                for(int j = 0; j < s[1].length(); ++j){
                    if(s[1].charAt(j) == '.'){
                        failMap(map, s[1].substring(j+1), a);
                    }
                }
            }
            List<String> res = new ArrayList<>();
            for(String s : map.keySet()){
                res.add(map.get(s) + " " + s);
            }
            return res;
        }
        private void failMap(Map<String, Integer> map, String s, int a){
            map.put(s, map.getOrDefault(s, 0)+a);
        }
    }
}